package Frame.screen;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import FCRendering.ImageUtil;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.FCPlayerInfo;
import VWorldGameStudio.FCRPlayerLink;
import VWorldGameStudio.FCTank;
import Function.FCItemForTankHelper;
import Function.FCMutilPlayCache;
import utils.FCUIConfig;
import utils.entity.UIConfigEntity;

import java.awt.*;
import java.util.Iterator;
import java.util.Map;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.*;
import static FCRendering.FCRMaster.fcv;

public class DefaultMenuScreen extends IScreen {
    Font font1, font2, font3, font4;

    @Override
    public void dispatchPaintComponent(Graphics g, FCRMaster fcrMaster) {
        // 渲染菜单
        if (fcv.isOpenMenu()) {
            if (!fcv.isBattle()) {
                g.drawImage(ImageUtil.getImage("gamemenuopen"), (int) (1856 * fcv.getProportion()),
                        (int) (1016 * fcv.getProportion()), (int) (64 * fcv.getProportion()), (int) (64 * fcv.getProportion()), this);
            }
            g.drawImage(ImageUtil.getImage("gamemenu"), (int) (fcv.getProportion() * 373), (int) (fcv.getProportion() * 202.5),
                    (int) (1174 * fcv.getProportion()), (int) (675 * fcv.getProportion()), this);// 菜单

            switch (fcv.getOpenMenuNum()) {
                case 0:
                    int offsetY = (int) (50 * fcv.getProportion());
                    for (int i = 0; i < FCMutilPlayCache.getTeamMembers().size() + 1; i++) {
                        FCPlayerInfo p = null;
                        if (i == 0) {
                            p = player1;
                        } else {
                            FCRPlayerLink link = FCMutilPlayCache.getTeamMembers().get(i - 1);
                            if (link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                                p = FCMutilPlayCache.findPlayerByName(gameSave.HOST_UID, link.curPlayerName);
                            }
                        }
                        if (p != null) {
                            //TODO 是否正在移动这个人的装备，要是正在移动那就不要画对应的装备
                            boolean isMovingThisManArmor = (fcv.isMoveKnapsackItem() && fcv.getTakeOffArmorType() >= 0
                                    && fcv.getKnapsackItemType() == 1 && p.getName().equals(fcv.getKnapsackItemWho()));
                            g.drawImage(ImageUtil.getImage("gamemenucha"), (int) (fcv.getProportion() * 388),
                                    (int) (fcv.getProportion() * 217.5 + offsetY), (int) (829 * fcv.getProportion()), (int) (112 * fcv.getProportion()), this);// 菜单-人物
                            g.drawImage(charLib.getCharacterOfName(p.getSelectPlayer()).getActionImage(0, 0, p.isDeath()),
                                    (int) (fcv.getProportion() * 389), (int) (fcv.getProportion() * 218.5 + offsetY), (int) (110 * fcv.getProportion()),
                                    (int) (110 * fcv.getProportion()), this);// 人物
                            if (p.getHat() != null && !p.getHat().equals("null") && !(isMovingThisManArmor && fcv.getTakeOffArmorType() == 1)) {
                                g.drawImage(itemLib.getItemOfID(p.getHat()).getPngPng(), (int) (505 * fcv.getProportion()),
                                        (int) (218.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()), (int) (42 * fcv.getProportion()), this);// 帽子装备
                            }
                            if (p.getJacket() != null && !p.getJacket().equals("null") && !(isMovingThisManArmor && fcv.getTakeOffArmorType() == 2))
                                g.drawImage(itemLib.getItemOfID(p.getJacket()).getPngPng(),
                                        (int) (574 * fcv.getProportion()), (int) (218.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()),
                                        (int) (42 * fcv.getProportion()), this);// 上衣装备
                            if (p.getPants() != null && !p.getPants().equals("null") && !(isMovingThisManArmor && fcv.getTakeOffArmorType() == 3))
                                g.drawImage(itemLib.getItemOfID(p.getPants()).getPngPng(), (int) (505 * fcv.getProportion()),
                                        (int) (287.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()), (int) (42 * fcv.getProportion()), this);// 裤子装备
                            if (p.getShoes() != null && !p.getShoes().equals("null") && !(isMovingThisManArmor && fcv.getTakeOffArmorType() == 4))
                                g.drawImage(itemLib.getItemOfID(p.getShoes()).getPngPng(), (int) (574 * fcv.getProportion()),
                                        (int) (287.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()), (int) (42 * fcv.getProportion()), this);// 鞋子装备

                            if (p.getArm() != null && !p.getArm().equals("null") && !(isMovingThisManArmor && fcv.getTakeOffArmorType() == 0)) {// 武器装备
                                g.drawImage(ImageUtil.getImage("gamemenuchaarm"), (int) (538 * fcv.getProportion()),
                                        (int) (251.5 * fcv.getProportion() + offsetY), (int) (44 * fcv.getProportion()), (int) (44 * fcv.getProportion()), this);// 武器装备背景
                                g.drawImage(itemLib.getItemOfID(p.getArm()).getPngPng(), (int) (539 * fcv.getProportion()),
                                        (int) (252.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()), (int) (42 * fcv.getProportion()), this);// 武器装备
                            }

                            g.drawImage(ImageUtil.getImage("game_glove_bg"), (int) (458 * fcv.getProportion()),
                                    (int) (287.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()), (int) (42 * fcv.getProportion()), this);// 手套装备
                            if (p.getGlove() != null && !p.getGlove().equals("null") && !(isMovingThisManArmor && fcv.getTakeOffArmorType() == 5)) {
                                g.drawImage(itemLib.getItemOfID(p.getGlove()).getPngPng(), (int) (458 * fcv.getProportion()),
                                        (int) (287.5 * fcv.getProportion() + offsetY), (int) (42 * fcv.getProportion()), (int) (42 * fcv.getProportion()), this);// 手套装备
                            }
                            g.setColor(Color.white);
                            g.drawString(p.getChName(), (int) (fcv.getProportion() * 708),
                                    (int) (fcv.getProportion() * 244.5 + offsetY));// 昵称
                            g.drawString(String.valueOf(p.getLevel()), (int) (fcv.getProportion() * 728),
                                    (int) (fcv.getProportion() * 282.5 + offsetY));// 等级
                            g.drawString(String.valueOf(p.getExperience()), (int) (fcv.getProportion() * 698),
                                    (int) (fcv.getProportion() * 320.5 + offsetY));// 经验
                            g.drawString(String.valueOf(p.getIntelligence()), (int) (fcv.getProportion() * 918),
                                    (int) (fcv.getProportion() * 244.5 + offsetY));// 智力
                            g.drawString(String.valueOf(p.getCrit()), (int) (fcv.getProportion() * 918),
                                    (int) (fcv.getProportion() * 282.5 + offsetY));// 战斗
                            g.drawString(String.valueOf(p.getStrength()), (int) (fcv.getProportion() * 918),
                                    (int) (fcv.getProportion() * 320.5 + offsetY));// 强度
                            g.drawString(String.valueOf(p.getSpeed1()), (int) (fcv.getProportion() * 1073),
                                    (int) (fcv.getProportion() * 244.5 + offsetY));// 速度
                            g.drawString(String.valueOf(p.getRepair()), (int) (fcv.getProportion() * 1073),
                                    (int) (fcv.getProportion() * 282.5 + offsetY));// 修理
                            g.drawString(String.valueOf(p.getSpeed2()), (int) (fcv.getProportion() * 1073),
                                    (int) (fcv.getProportion() * 320.5 + offsetY));// 驾驶
                            g.drawString(String.valueOf(p.getPhysical()), (int) (fcv.getProportion() * 1228),
                                    (int) (fcv.getProportion() * 244.5 + offsetY));// 体力
                            g.drawString(String.valueOf(p.getPlayerAttackHurt()), (int) (fcv.getProportion() * 1228),
                                    (int) (fcv.getProportion() * 282.5 + offsetY));// 攻击
                            g.drawString(String.valueOf(p.getPlayerWuliDefend()), (int) (fcv.getProportion() * 1228), (int) (fcv.getProportion() * 320.5 + offsetY));// 防御
                            offsetY += (int) (145 * fcv.getProportion());
                        }
                    }
                    break;
                case 1:
                    g.drawImage(ImageUtil.getImage("gamemenutank"), (int) ((fcrMaster.getWidth() - fcv.getProportion() * 1144) / 2),
                            (int) ((fcrMaster.getHeight() - fcv.getProportion() * 645) / 2), (int) (935 * fcv.getProportion()),
                            (int) (644 * fcv.getProportion()), this);// 菜单-战车
                    FCTank tank = null;
                    if (fcv.getTanknameList().size() > 0) {
                        tank = tankLib.getTankOfID(fcv.getTanknameList().get(fcv.getShowTankMenuTankIndex()),
                                fcv.getTankHostList().get(fcv.getShowTankMenuTankIndex()));
                    }
                    if (tank != null) {
                        g.setColor(Color.white);
                        g.setFont(font4);
                        UIConfigEntity entity = FCUIConfig.getIn().getByKey("tankMenuHead");
                        //int[] size = tank.getImageMasterSize();e
                        g.drawImage(tank.getImageMaster(), entity.getUiRect().x,
                                entity.getUiRect().y, entity.getUiRect().width,
                                entity.getUiRect().height, this);
                        FCItem item = null;
                        item = itemLib.getItemOfID(tank.getGun2ID());
                        boolean isDragArmor = (fcv.isMoveKnapsackItem() && fcv.getKnapsackItemType() == 2 &&
                                tank == fcv.getDragBagWitchTank() && fcv.getKnapsackItemNum() == -1);
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuGun2");
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 1)) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuGun2Text");
                            g.drawString(tank.getGun2Text(item), entity.getUiRect().x, entity.getUiRect().y);
                        }
                        item = itemLib.getItemOfID(tank.getCdeviceID());
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuC");
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 2)) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuCText");
                            g.drawString(tank.getCText(item), entity.getUiRect().x, entity.getUiRect().y);
                        }
                        item = itemLib.getItemOfID(tank.getChassisID());
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuCA");
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 4)) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuCAText");
                            g.drawString(tank.getCAText(item), entity.getUiRect().x, entity.getUiRect().y);
                            entity = FCUIConfig.getIn().getByKey("tankMenuShellText");
                            g.drawString(FCItemForTankHelper.getAllShellWeight(item) + "t", entity.getUiRect().x, entity.getUiRect().y);
                            //弹仓显示
                            for (int i = 1; i < 9; i++) {
                                entity = FCUIConfig.getIn().getByKey("tankMenuSellCase" + i);
                                if (i > item.getShellCaseNum()) {
                                    g.drawImage(ImageUtil.getImage("disable"), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                                } else {
                                    FCItem shell = itemLib.getItemOfID(item.getShellCaseType()[i - 1]);
                                    if (shell != null) {
                                        g.drawImage(shell.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                                        //这里还要显示特殊弹的数量
                                        g.drawString(String.valueOf(item.getShellCount()[i - 1]), entity.getUiRect().x, entity.getUiRect().y + entity.getUiRect().height);
                                    }
                                }
                            }
                        } else {
                            for (int i = 1; i < 9; i++) {
                                entity = FCUIConfig.getIn().getByKey("tankMenuSellCase" + i);
                                g.drawImage(ImageUtil.getImage("disable"), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                        }
                        item = itemLib.getItemOfID(tank.getSEID());
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuSE");
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 6)) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuSEText");
                            g.drawString(tank.getSEText(item), entity.getUiRect().x, entity.getUiRect().y);
                        }
                        item = itemLib.getItemOfID(tank.getEngineID());
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuE");
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 3)) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuEText");
                            g.drawString(tank.getEText(item), entity.getUiRect().x, entity.getUiRect().y);
                        }
                        item = itemLib.getItemOfID(tank.getRadarID());
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuSC");
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 5)) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuSCText");
                            g.drawString(tank.getSCText(item), entity.getUiRect().x, entity.getUiRect().y);
                        }
                        item = itemLib.getItemOfID(tank.getGunID());
                        if (item != null) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuGun1");
                            g.drawImage(ImageUtil.getImage("gamegun1bg"), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            if (!(isDragArmor && fcv.getTakeOffArmorType() == 0)) {
                                g.drawImage(item.getPngPng(), (int) (entity.getUiRect().x + 6 * fcv.getProportion()), (int) (entity.getUiRect().y + 6 * fcv.getProportion()),
                                        (int) (entity.getUiRect().width - 12 * fcv.getProportion()), (int) (entity.getUiRect().height - 12 * fcv.getProportion()), this);
                            }
                            entity = FCUIConfig.getIn().getByKey("tankMenuGun1Text");
                            g.drawString(tank.getGun1Text(item), entity.getUiRect().x, entity.getUiRect().y);
                        }

                        boolean isMovingBagItem = (fcv.isMoveKnapsackItem() && fcv.getKnapsackItemType() == 2 &&
                                tank == fcv.getDragBagWitchTank() && fcv.getKnapsackItemNum() >= 0);
                        //坦克背包展示
                        for (int i = 1; i < 11; i++) {
                            entity = FCUIConfig.getIn().getByKey("tankMenuBagCell" + i);
                            item = itemLib.getItemOfID(tank.getKnapsack()[i - 1]);
                            if (item != null && !(isMovingBagItem && (i - 1) == fcv.getKnapsackItemNum())) {
                                g.drawImage(item.getPngPng(), entity.getUiRect().x, entity.getUiRect().y, entity.getUiRect().width, entity.getUiRect().height, this);
                            }
                        }
                        entity = FCUIConfig.getIn().getByKey("tankMenuName");
                        g.drawString(tank.getName(), entity.getUiRect().x, entity.getUiRect().y);
                        entity = FCUIConfig.getIn().getByKey("tankMenuSPText");
                        g.drawString(tank.getSPText(), entity.getUiRect().x, entity.getUiRect().y);
                        entity = FCUIConfig.getIn().getByKey("tankMenuEWText");
                        g.drawString(tank.getEWText(), entity.getUiRect().x, entity.getUiRect().y);
                        entity = FCUIConfig.getIn().getByKey("tankMenuAWText");
                        g.drawString(tank.getAWText(), entity.getUiRect().x, entity.getUiRect().y);
                        entity = FCUIConfig.getIn().getByKey("tankMenuDetail");
                        String detail = fcv.getShowingTankMenuDetail() == null ? tank.getDetail() : fcv.getShowingTankMenuDetail();
                        g.drawString(detail, entity.getUiRect().x, entity.getUiRect().y);
                    }
                    break;
                case 4:
                    g.drawImage(ImageUtil.getImage("gamemenusta"), (int) ((fcrMaster.getWidth() - fcv.getProportion() * 1174) / 2),
                            (int) ((fcrMaster.getHeight() - fcv.getProportion() * 675) / 2), (int) (1174 * fcv.getProportion()),
                            (int) (675 * fcv.getProportion()), this);// 菜单-统计
                    break;
                case 5:
                    g.drawImage(ImageUtil.getImage("gamemenuset"), (int) ((fcrMaster.getWidth() - fcv.getProportion() * 1174) / 2),
                            (int) ((fcrMaster.getHeight() - fcv.getProportion() * 675) / 2), (int) (1174 * fcv.getProportion()),
                            (int) (675 * fcv.getProportion()), this);// 菜单-设置
                    g.setColor(Color.white);
                    g.setFont(font3);
                    String qwe1 = hotkey.getKeyNameOfCodeKey("up", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (316 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("down", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (355 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("left", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (394 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("right", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (433 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("say", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (472 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("lookinto", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (511 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("give", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (550 * fcv.getProportion()));
                    qwe1 = hotkey.getKeyNameOfCodeKey("menu", true);
                    g.drawString(qwe1, (int) ((570 - qwe1.length() * 15) * fcv.getProportion()), (int) (589 * fcv.getProportion()));
                    // 描述
                    g.setFont(font1);
                    g.drawString("重装机兵第一代重制版", (int) (720 * fcv.getProportion()), (int) (270 * fcv.getProportion()));
                    g.setFont(font2);
                    g.drawString("由  V-World Game Studio 开发", (int) (725 * fcv.getProportion()), (int) (310 * fcv.getProportion()));
                    g.drawString("主要开发 孤独的旅者,bpbwan", (int) (730 * fcv.getProportion()), (int) (340 * fcv.getProportion()));
                    g.drawString("剧情设计 伙伴狼、以生誓不脱宅等", (int) (720 * fcv.getProportion()), (int) (370 * fcv.getProportion()));
                    g.drawString("版本 V0.9.4 2022", (int) (785 * fcv.getProportion()), (int) (400 * fcv.getProportion()));
                    break;
                case 7:
                    //坦克坐标
                    int linesY = (int) (260 * fcv.getProportion());
                    int lineX = (int) (fcv.getProportion() * 410);
                    g.setFont(font1);
                    g.setColor(Color.WHITE);
                    g.drawString("已有坦克列表:", lineX, linesY);
                    linesY += (int) (50 * fcv.getProportion());
                    Iterator<Map.Entry<String, FCTank>> iterator = gameSave.getTankSaveIos().entrySet().iterator();
                    while (iterator.hasNext()) {
                        FCTank tank2 = iterator.next().getValue();
                        g.drawString(langLib.getNameOfId(tank2.getName()) + " 所在地图：" + langLib.getNameOfId(tank2.getPmapname()) +
                                " 坐标：" + tank2.getPcoorX() + "*" + tank2.getPcoorY(), lineX, linesY);
                        linesY += (int) (50 * fcv.getProportion());
                    }
                    break;
            }
            g.setColor(new Color(163, 163, 163));
            // 菜单大选项
            for (int i = 0; i < 8; i++)
                if (fcv.getOpenMenuNum() == i || fcv.getEnterBu() - 501 == i)
                    g.fillRect((int) (1364 * fcv.getProportion()), (int) ((221 + 43 * i) * fcv.getProportion()),
                            (int) (16 * fcv.getProportion()), (int) (34 * fcv.getProportion()));

            // 菜单详细按钮
            UIConfigEntity entity = FCUIConfig.getIn().getEntityByEnterBu(fcv.getEnterBu());
            if (entity != null) {
                g.fillRect(entity.getSelectBuX(), entity.getSelectBuY(), (int) (16 * fcv.getProportion()),
                        (int) (34 * fcv.getProportion()));
            }
        } else {
            if (!fcv.isBattle())
                g.drawImage(ImageUtil.getImage("gamemenuclose"), (int) (fcrMaster.getWidth() - 64 * fcv.getProportion()),
                        (int) (fcrMaster.getHeight() - 64 * fcv.getProportion()), (int) (64 * fcv.getProportion()), (int) (64 * fcv.getProportion()),
                        this);
        }
    }

    @Override
    public void setBounds() {
        font1 = new Font(FCRConstants.FONT_BLACK, 0, (int) (30 * fcv.getProportion()));
        font2 = new Font(FCRConstants.FONT_BLACK, 0, (int) (20 * fcv.getProportion()));
        font3 = new Font(FCRConstants.FONT_BLACK, 0, (int) (25 * fcv.getProportion()));
        font4 = new Font(FCRConstants.FONT_BLACK, 0, (int) (27 * fcv.getProportion()));
    }
}
